<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<c:import url="/WEB-INF/jsp/head.jsp"/>
</head>
<body>

	<c:import url="/WEB-INF/jsp/cabecalho.jsp"/>
	
	<h3 align="center">Cadastro de Questionário</h3>
	
	<form method="post" action="<c:url value="/questionario/salvar"/>">
	
		<c:if test="${not empty errors}">
			<div id="erros" style="color:red" class="erros">
				Foram encontrados erros no cadastro:<br/>
				<ul>
				<c:forEach var="error" items="${errors}">
				    <li>${error.message}</li>
				</c:forEach>
				</ul>
			</div>
		</c:if>
		
		<%-- Atribuição das variáveis para usar nos inputs (evitar o nullPointerException) --%>
		<c:set scope="request" var="alteracao" value="false"/>
		<c:if test="${not empty questionario}">
			<c:set scope="request" var="questionarioId" value="${questionario.id}"/>
			<c:set scope="request" var="objetivo" value="${questionario.objetivo}"/>
			<c:set scope="request" var="divisoes" value="${questionario.divisaoQuestionarios}"/>
			<c:set scope="request" var="alteracao" value="true"/>
		</c:if>
	
		<table align="center" width="100%" border="0">
			<tr>
				<td width="50%" align="right">Id:</td>
				<td width="10"></td>
				<td width="50%" align="left">
					<input type="text" name="questionario.id" id="questionario.id" value="${questionarioId}" style="width:100px" />
				</td>
			</tr>
			<tr>
				<td width="50%" align="right">Objetivo:</td>
				<td width="10"></td>
				<td width="50%" align="left"><input type="text" name="questionario.objetivo" id="questionario.objetivo" value="${objetivo}" style="width:400px"/></td>
			</tr>
		</table>
		
		<input type="hidden" name="alteracao" id="alteracao" value="${alteracao}" />
		
		<br />
		
		<div id="divisoes" align="center">
			<c:if test="${not empty divisoes}">
				<table width="600" border="0">
				
					<c:forEach var="divisao" items="${divisoes}">
						
						<thead>
							<tr>
								<td colspan="3" align="left"><strong><i>${divisao.descricao}</i></strong></td>
								<td width="20" align="center">
									<a href="javascript: window.location='<c:url value="/questionario/subdivisao/${questionario.id}/${divisao.id}"/>'">
										<img src="<c:url value="/img/icone_editar.png"/>" alt="Editar" title="Editar" style="cursor: pointer" border="0"/>
									</a>
								</td>
								<td width="20" align="center">
									<a href="javascript: excluir('<c:url value="/questionario/subdivisao"/>', '${questionario.id}/${divisao.id}')">
										<img src="<c:url value="/img/icone_excluir.png"/>" alt="Excluir" title="Excluir" style="cursor: pointer" border="0"/>
									</a>
								</td>
						</tr>
						</thead>
						
						<c:if test="${not empty divisao.questoes}">
						
							<tbody class="zebrado">
							
								<c:forEach var="questao" items="${divisao.questoes}">
								
									<tr>
										<td width="20" style="background-color: #FFF;">&nbsp;</td>
										<td width="80" align="center">${questao.id}</td>
										<td colspan="3" align="left">${questao.descricao}</td>
									</tr>
								
								</c:forEach>
								
							</tbody>
						
						</c:if>
						
					</c:forEach>
					
				</table>
			</c:if>
		</div>		
		
		<br /><br />
		
		<div align="center">
			<c:if test="${not empty questionario}">
				<input type="button" name="btnAdicionar" id="btnAdicionar" value="Adicionar divisão" onclick="javascript: window.location='<c:url value="/questionario/subdivisao/${questionario.id}/0"/>'"/>
			</c:if>
			<input type="submit" name="btnSalvar" id="btnSalvar" value="Salvar" />
			<input type="reset" name="btnLimpar" id="btnLimpar" value="Limpar" />
		</div>
	</form>
	
	<div align="center">
		<br /><br /><br />
		<a href="<c:url value="/questionario"/>">Voltar</a>
	</div>
	
	<c:import url="/WEB-INF/jsp/rodape.jsp"/>
	
</body>
</html>